#include<stdio.h>
#include<gmp.h>
#include<stdlib.h>

int main(void)
{	//declaraçoẽs
	mpf_t a,y,aux1,aux2,pi;
	int i;
	FILE *f;
	//alocacao na mem.
	mpf_init(a);
	mpf_init(y);
	mpf_init(aux1);
	mpf_init(aux2);
	mpf_init(pi);
	mpf_set_prec (a ,33300000);//reserva um numero de 1428554 bits para a.
	mpf_set_prec (y ,33300000);
	mpf_set_prec (aux1 ,33300000);
	mpf_set_prec (aux2 ,33300000);
	mpf_set_prec (pi ,33300000);
	// iniciação das variáveis.
	mpf_set_d(aux1,2.0);
	mpf_sqrt(aux1,aux1);
	mpf_set(y,aux1);
	mpf_mul_ui(aux1,aux1,4);
	mpf_set_d(a,6.0);
	mpf_sub(a,a,aux1);
	mpf_sub_ui(y,y,1);
	mpf_set_d(pi,1.0);
	/*gmp_printf("a = %F1.10f\n", a);
	gmp_printf("y = %F1.10f\n", y);
	gmp_printf("pi = %F1.10f\n", pi);*/
	for(i=0;i<23;i++)// precisao quadratica, 2^22 < 10.000.000 < 2^23
	{
		//calculo de y(n+1)
		mpf_set_d(aux2,1.0);
		mpf_pow_ui(aux1,y,4);
		mpf_sub(aux1,aux2,aux1);
		mpf_sqrt(aux1,aux1);
		mpf_sqrt(aux1,aux1);
		mpf_sub(y,aux2,aux1);
		mpf_add(aux1,aux2,aux1);
		mpf_div(y,y,aux1);

		//calculo de a(n+1)
		mpf_set(aux1,y);
		mpf_add_ui(aux1,aux1,1);
		mpf_pow_ui(aux1,aux1,4);
		mpf_mul(a,aux1,a);
		mpf_set_d(aux1,2.0);
		mpf_pow_ui(aux1,aux1,((2*i)+3));
		mpf_pow_ui(aux2,y,2);
		mpf_add(aux2,aux2,y);
		mpf_add_ui(aux2,aux2,1);
		mpf_mul(aux2,aux2,y);
		mpf_mul(aux2,aux2,aux1);
		mpf_sub(a,a,aux2);
	
		//gmp_printf("y = %F1.50f\n",y);
		//gmp_printf("a = %F1.50f\n",a);	
	}
	mpf_div(pi,pi,a);
	f=fopen("saida.txt","w");
	gmp_fprintf (f, "%.10000000Ff\n",pi);

	return 0;
}
